package com.anyou.mall.goo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.anyou.common.annotation.ShopScope;
import com.anyou.common.core.domain.entity.SysUser;
import com.anyou.mall.goo.domain.GoodsCouponHistory;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.param.SendGoodsCouponParam;
import com.anyou.mall.order.service.IOrdOrderService;
import com.anyou.mall.parts.domain.PlaPartsClassify;
import com.anyou.mall.parts.service.IPlaPartsClassifyService;
import com.anyou.mall.use.domain.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.anyou.mall.goo.domain.GoodsCoupon;
import com.anyou.mall.goo.service.IGoodsCouponService;
import com.anyou.common.utils.poi.ExcelUtil;
import com.anyou.common.core.page.TableDataInfo;

/**
 * 商品优惠券Controller
 *
 * @author gaochaohao
 * @date 2022-03-28
 */
@Api(tags = "商品优惠券")
@RestController
@RequestMapping("/goo/goodsCoupon")
public class GoodsCouponController extends BaseController
{
    @Autowired
    private IGoodsCouponService goodsCouponService;

    @Autowired
    private IPlaPartsClassifyService plaPartsClassifyService;
    @Autowired
    private IOrdOrderService orderService;

    /**
     * 查询商品优惠券列表
     */
    @ApiOperation("查询商品优惠券列表")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:list')")
    @GetMapping("/list")
    @ShopScope(baseTableAlias = "c")
    public TableDataInfo<List<GoodsCoupon>> list(GoodsCoupon goodsCoupon)
    {
        startPage();
        List<GoodsCoupon> list = goodsCouponService.selectGoodsCouponList(goodsCoupon);
        return getDataTable(list);
    }

    /**
     * 优惠卷使用记录
     */
    @ApiOperation("优惠卷使用记录")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:useRecord')")
    @GetMapping("/useRecord/{goodsCouponId}")
    public TableDataInfo<List<GoodsCouponHistory>> useRecord(@PathVariable("goodsCouponId") Long goodsCouponId)
    {
        startPage();
        List<GoodsCouponHistory> list = goodsCouponService.useRecord(goodsCouponId);
        return getDataTable(list);
    }

    /**
     * 导出商品优惠券列表
     */
    @ApiOperation("导出商品优惠券列表")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:export')")
    @Log(title = "商品优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ShopScope(baseTableAlias = "b")
    public void export(GoodsCoupon goodsCoupon,HttpServletResponse response)
    {
        List<GoodsCoupon> list = goodsCouponService.selectGoodsCouponList(goodsCoupon);
        ExcelUtil<GoodsCoupon> util = new ExcelUtil<GoodsCoupon>(GoodsCoupon.class);
        util.exportExcel(response, list, "商品优惠券数据");
    }

    /**
     * 获取商品优惠券详细信息
     */
    @ApiOperation("获取商品优惠券详细信息")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:query')")
    @GetMapping(value = "/{goodsCouponId}")
    public ApiResult<GoodsCoupon> getInfo(@PathVariable("goodsCouponId") Long goodsCouponId)
    {
        return ApiResult.success(goodsCouponService.selectGoodsCouponByGoodsCouponId(goodsCouponId));
    }

    /**
     * 新增商品优惠券
     */
    @ApiOperation("新增商品优惠券")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:add')")
    @Log(title = "商品优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult add(@RequestBody GoodsCoupon goodsCoupon)
    {
        Long userId = getUserId();
        SysUser user = orderService.selectUserById(userId);
        if(user!=null&&user.getCarPartsShopId()!=null){
            goodsCoupon.setCarPartsShopId(user.getCarPartsShopId());
        }
        return ApiResult.success(goodsCouponService.insertGoodsCoupon(goodsCoupon));
    }

    /**
     * 修改商品优惠券
     */
    @ApiOperation("修改商品优惠券")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:edit')")
    @Log(title = "商品优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult edit(@RequestBody GoodsCoupon goodsCoupon)
    {
        return ApiResult.success(goodsCouponService.updateGoodsCoupon(goodsCoupon));
    }

    /**
     * 删除商品优惠券
     */
    @ApiOperation("删除商品优惠券")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:remove')")
    @Log(title = "商品优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsCouponIds}")
    public ApiResult remove(@PathVariable Long[] goodsCouponIds)
    {
        return ApiResult.success(goodsCouponService.deleteGoodsCouponByGoodsCouponIds(goodsCouponIds));
    }

    /**
     * 发送商品优惠券给用户
     */
    @ApiOperation("发送商品优惠券给用户")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:send')")
    @Log(title = "商品优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/send")
    public ApiResult send(@RequestBody SendGoodsCouponParam sendGoodsCouponParam)
    {
        return ApiResult.success(goodsCouponService.sendGoodsCouponByGoodsUserIds(sendGoodsCouponParam));
    }

    /**
     * 发送商品优惠券给用户
     */
    @ApiOperation("根据手机号名称用户查询")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:userList')")
    @PostMapping("/userList")
    public ApiResult userList(@RequestBody User user)
    {
        return ApiResult.success(goodsCouponService.userList(user));
    }

    /**
     * 发送商品优惠券给用户
     */
    @ApiOperation("可用商品查询")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:GoodsList')")
    @PostMapping("/goodsList")
    public ApiResult goodsList(@RequestBody GooGoods gooGoods)
    {
        return ApiResult.success(goodsCouponService.goodsList(gooGoods));
    }

    /**
     * 发送商品优惠券给用户
     */
    @ApiOperation("可用配件分类查询")
    @PreAuthorize("@ss.hasPermi('goo:goodsCoupon:classifyList')")
    @PostMapping("/classifyList")
    public ApiResult classifyList()
    {
        PlaPartsClassify plaPartsClassify = new PlaPartsClassify();
        return ApiResult.success(plaPartsClassifyService.selectPlaPartsClassifyList(plaPartsClassify));
    }
}
